#include "login.h"
#include "ui_login.h"
#include <QMessageBox>
#include <QIntValidator>
#include "userdb.h"

Login::Login(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);
    QIntValidator* intValidator = new QIntValidator{100000, 999999, this};
    // intValidator->set(100000, 999999);
    ui->lineEdit_login_id->setValidator(intValidator);
    ui->lineEdit_register_id->setValidator(intValidator);
    ui->lineEdit_modify_id->setValidator(intValidator);
    ui->lineEdit_modify_tel->setValidator(intValidator);
    ui->stackedWidget->setCurrentWidget(ui->page_login);
    ui->stackedWidget->setStyleSheet("background-image:url(:/Image/login_bg.jpg);");
    this->setWindowTitle("打卡界面");
    this->setWindowIcon(QIcon(":/Image/myheart.png"));
}

Login::~Login()
{
    delete ui;
}

/**
 * @brief Login::on_pushButton_login_register_clicked
 * 切换注册界面
 */
void Login::on_pushButton_login_register_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_register);
}


/**
 * @brief Login::on_pushButton_login_modify_clicked
 * 切换修改界面
 */
void Login::on_pushButton_login_modify_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_modify);
}

/**
 * @brief Login::on_pushButton_login_login_clicked
 * 提交登录的信息，完成登录操作
 */
void Login::on_pushButton_login_login_clicked()
{
    QString id = ui->lineEdit_login_id->text();
    QString password = ui->lineEdit_login_password->text();
    if (id.isEmpty() or password.isEmpty()) {
        QMessageBox::information(this, "tip", "账号或密码为空");
        return;
    }
    //进行登录检验
    if (UserDB::instance().userLogin(id, password)) {
        QMessageBox::information(this, "tip", "登录成功");
        emit login_success(id.toInt());
        ui->lineEdit_login_id->clear();
        ui->lineEdit_login_password->clear();
        this->hide();
        return;
    }
    //登录失败
    QMessageBox::information(this, "tip", "账号或密码错误");
}
/**
 * @brief Login::on_pushButton_register_login_clicked
 * 切换登录界面
 */
void Login::on_pushButton_register_login_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

/**
 * @brief Login::on_pushButton_modify_register_clicked
 * 切换注册界面
 */
void Login::on_pushButton_modify_register_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_register);
}

/**
 * @brief Login::on_pushButton_modify_login_clicked
 * 切换登录界面
 */
void Login::on_pushButton_modify_login_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

/**
 * @brief Login::on_pushButton_register_register_clicked
 * 用户注册
 */
void Login::on_pushButton_register_register_clicked()
{
    QString id = ui->lineEdit_register_id->text();
    QString password = ui->lineEdit_register_password->text();
    QString password_verify = ui->lineEdit_register_password_2->text();
    if (id.isEmpty() or password.isEmpty() or password_verify.isEmpty()) {
        QMessageBox::information(this, "tip", "账号或密码为空");
        return;
    }
    if (password != password_verify) {
        QMessageBox::information(this, "tip", "两次密码不一致");
        return;
    }
    if (UserDB::instance().userRegister(id, password)) {
        QMessageBox::information(this, "tip", "注册成功");
        return;
    }
    QMessageBox::information(this, "tip", "注册失败,用户存在");
}

/**
 * @brief Login::on_pushButton_modify_modify_clicked
 * 提交修改的信息
 */
void Login::on_pushButton_modify_modify_clicked()
{
    QString name = ui->lineEdit_modify_name->text();
    QString id = ui->lineEdit_modify_id->text();
    QString old_password = ui->lineEdit_modify_password->text();
    QString new_password = ui->lineEdit_modify_password_2->text();
    QString tel = ui->lineEdit_modify_tel->text();
    QString addr = ui->lineEdit_modify_addr->text();
    if (name.isEmpty()
        or id.isEmpty()
        or old_password.isEmpty()
        or new_password.isEmpty()
        or tel.isEmpty()
        or addr.isEmpty()
        )
    {
        QMessageBox::information(this, "tip", "请填写信息");
        return;
    }
    if (old_password == new_password) {
        QMessageBox::information(this, "tip", "两次密码一致");
        return;
    }
    if (UserDB::instance().userUpdateInfo(name, id, old_password, new_password, tel, addr)) {
        QMessageBox::information(this, "tip", "修改信息成功");
        return;
    }
    QMessageBox::information(this, "tip", "修改信息失败");
}

/**
 * @brief Login::on_pushButton_modify_delete_clicked
 * 删除用户
 */
void Login::on_pushButton_modify_delete_clicked()
{

}

/**
 * @brief Login::on_pushButton_admin_login_clicked
 * 管理员登录界面
 */
void Login::on_pushButton_admin_login_clicked()
{
    QString id = ui->lineEdit_admin_id->text();
    QString password = ui->lineEdit_admin_password->text();
    if (id.isEmpty() or password.isEmpty()) {
        QMessageBox::information(this, "tip", "账号或密码为空");
        return;
    }
    if (id == "123456" and password == "123456") {
        QMessageBox::information(this, "tip", "登录成功");
        this->hide();
        emit this->admin_login_success();
        ui->lineEdit_admin_id->clear();
        ui->lineEdit_admin_password->clear();
    }
}

/**
 * @brief Login::on_pushButton_login_admin_login_clicked
 * 切换到管理员登录界面
 */
void Login::on_pushButton_login_admin_login_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_admin);
}

/**
 * @brief Login::on_pushButton_admin_back_clicked
 * 返回登录界面
 */
void Login::on_pushButton_admin_back_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_login);
}

